package com.hnzq.cloud.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnzq.cloud.commons.TransferBeanUtil;
import com.hnzq.cloud.dto.FreImgDto;
import com.hnzq.cloud.entity.FreGoods;
import com.hnzq.cloud.entity.FreGoodsImg;
import com.hnzq.cloud.entity.FreImg;
import com.hnzq.cloud.exception.FreGoodsException;
import com.hnzq.cloud.exception.FreGoodsImgException;
import com.hnzq.cloud.exception.FreImgException;
import com.hnzq.cloud.mapper.FreGoodsImgMapper;
import com.hnzq.cloud.mapper.FreGoodsMapper;
import com.hnzq.cloud.mapper.FreImgMapper;
import com.hnzq.cloud.param.AddFreGoodsImgParam;
import com.hnzq.cloud.param.AddFreImgParam;
import com.hnzq.cloud.param.AddFreImgsParam;
import com.hnzq.cloud.param.DeleteImgsParam;
import com.hnzq.cloud.service.FreGoodsImgService;
import com.hnzq.cloud.service.FreImgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jams
 * @since 2022-02-06
 */
@Service
public class FreImgServiceImpl extends ServiceImpl<FreImgMapper, FreImg> implements FreImgService {

    @Resource
    private FreGoodsImgMapper freGoodsImgMapper;

    @Resource
    private FreGoodsMapper freGoodsMapper;

    @Resource
    private FreGoodsImgService freGoodsImgService;

    @Override
    public Boolean addFreImg(AddFreImgsParam addFreImgsParam){
        if (addFreImgsParam.getImgParams()==null) throw new FreImgException("无添加的图片",344);
        List<AddFreImgParam> imgParams = addFreImgsParam.getImgParams();

        List<FreImg> freImgs = TransferBeanUtil.copyList(imgParams, FreImg.class);
        saveBatch(freImgs);

        AddFreGoodsImgParam addFreGoodsImgParam = new AddFreGoodsImgParam();
        addFreGoodsImgParam.setGoodsId(addFreImgsParam.getGoodsId());
        List<Integer> imgIds = freImgs.stream().map(item -> item.getImgId()).collect(Collectors.toList());
        addFreGoodsImgParam.setImgIds(imgIds);
        return freGoodsImgService.addFreGoodsImgs(addFreGoodsImgParam);
    }

    @Override
    public List<FreImgDto> findFreGoodsImgs(Integer id) {
        FreGoods freGoods = freGoodsMapper.selectById(id);
        if (freGoods==null) throw new FreGoodsException("查询商品不存在",144);
        QueryWrapper<FreGoodsImg> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goods_id",id);
        List<FreGoodsImg> freGoodsImgs = freGoodsImgMapper.selectList(queryWrapper);
        if (freGoodsImgs==null||freGoodsImgs.size()==0) throw new FreGoodsImgException("暂无商品关联图片",312);
        List<Integer> imgIds = freGoodsImgs.stream().map(item -> item.getImgId()).collect(Collectors.toList());
        List<FreImg> freImgs = this.baseMapper.selectBatchIds(imgIds);
        if (freImgs==null) throw new FreImgException("暂无相关图片",311);
        if (freImgs.size()!=imgIds.size()) throw  new FreImgException("存在图片缺失",322);
        List<FreImgDto> freImgDtos = TransferBeanUtil.copyList(freImgs, FreImgDto.class);
        return freImgDtos;
    }

    @Override
    public Boolean deleteFreImg(DeleteImgsParam deleteImgsParam){
        FreGoods freGoods = freGoodsMapper.selectById(deleteImgsParam.getGoodsId());
        if (freGoods==null) throw new FreGoodsException("删除照片的商品不存在",144);
        List<Integer> imgIds = deleteImgsParam.getImgIds();
        List<FreImg> freImgs = this.baseMapper.selectBatchIds(imgIds);
        if (freImgs==null) throw new FreImgException("无可删除的图片",344);
        if (freImgs.size()!=imgIds.size()) throw new FreImgException("参数有误",322);
        removeByIds(imgIds);

        QueryWrapper<FreGoodsImg> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",deleteImgsParam.getGoodsId())
                .in("img_id",deleteImgsParam.getImgIds());
        int count = freGoodsImgMapper.delete(wrapper);
        if (count>0) return true;
        return false;
    }


}
